# -*- coding: utf-8 -*-
import ast
from core.response import JsonResponse
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from apps.tools.general.utils.formatter import *
from apps.tools.general.utils.generater import *


class GeneralView(APIView):
    """常用工具"""
    permission_classes = [AllowAny]

    def post(self, request, *args, **kwargs):
        try:
            type = request.data.get('type')
            inputs = request.data.get('inputs')
            user_id = request.data.get('userId')
            func_name = request.data.get('funcName')
            func_params = request.data.get('funcParams')
            if (type == 'customize'):
                data = eval(func_name)(inputs, user_id, **func_params)
            else:
                data = list(map(lambda x: eval(func_name)
                            (x), ast.literal_eval(inputs)))
            return JsonResponse(data=data, msg="运行成功")
        except Exception as ex:
            return JsonResponse(code=500, data=[], msg=f"运行失败：{str(ex)}")
